﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IBMS.Common;

namespace IBMS.Page
{
    public partial class ProjectModify : System.Web.UI.Page
    {
        private DB db = new DB();
        private string modifyProjectId="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Div1.Visible = true;
                this.Div2.Visible = false;
                this.Div3.Visible = false;
                if (Request.QueryString["ProjectId"] != null)
                {
                    modifyProjectId = Request.QueryString["ProjectId"].ToString();

                    DataTable da = db.GetDataTable("select * from Project where ProjectId='"+modifyProjectId+"'");
                    this.projectId.Text = da.Rows[0]["ProjectId"].ToString();
                    this.name.Text = da.Rows[0]["Name"].ToString();
                    this.version.Text = da.Rows[0]["Version"].ToString();

                    DataTable da1 = db.GetDataTable("select * from SonSystem where Father='"+modifyProjectId+"'");
                    for (int i = 0; i < da1.Rows.Count; i++)
                    {
                        ListItem item = new ListItem();
                        item.Text = da1.Rows[i]["Name"].ToString();
                        item.Value = da1.Rows[i]["Name"].ToString();
                        item.Selected = false;
                        this.DropDownList1.Items.Add(item);
                        item.Selected = true;
                        this.CheckBoxList1.Items.Add(item);
                    }
                    ListItem temitem = new ListItem();
                    temitem.Text = "点击选择";
                    temitem.Value = "点击选择";
                    this.DropDownList1.Items.Add(temitem);

                    DataTable da2 = db.GetDataTable("select * from Users where ProjectId like '%"+modifyProjectId+"%'");
                    for (int i = 0; i < da2.Rows.Count; i++)
                    {
                        ListItem item = new ListItem();
                        item.Value = da2.Rows[i]["Name"].ToString();
                        item.Text = da2.Rows[i]["Name"].ToString();
                        this.DropDownList2.Items.Add(item);
                    }
                    ListItem temitem1 = new ListItem();
                    temitem1.Text = "点击选择";
                    temitem1.Value = "点击选择";
                    this.DropDownList2.Items.Add(temitem1);
                    this.DropDownList1.ClearSelection();
                    this.DropDownList2.ClearSelection();

                }
            }
        }

        protected void btn_zhuce_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ProjectId"] != null)
            {
                modifyProjectId = Request.QueryString["ProjectId"].ToString();
            }
            if (IsValid)
            {
                string projectId = this.projectId.Text.ToString();
                string name = this.name.Text.ToString();
                string version = this.version.Text.ToString();
                bool result=db.ExecuteNonQury("update Project set ProjectId='"+projectId+"' ,Name='"+name+"' ,Version="+int.Parse(version)+" where ProjectId='"+modifyProjectId+"'");
                DataTable daa = db.GetDataTable("select * from Users where ProjectId like '%" + modifyProjectId + "%'");
                for (int i = 0; i < daa.Rows.Count; i++)
                {
                    result = db.ExecuteNonQury("update Users set ProjectId='"+daa.Rows[i]["ProjectId"].ToString().Replace(modifyProjectId+";","")+"'");
                }
                for (int i = 0; i < this.DropDownList2.Items.Count-1; i++)
                {
                    DataTable dat = db.GetDataTable("select * from Users where Name='"+this.DropDownList2.Items[i].Text.ToString()+"'");
                    result = db.ExecuteNonQury("update Users set ProjectId='" + projectId + ";" + dat.Rows[0]["ProjectId"].ToString() + "' where Name='" + this.DropDownList2.Items[i].Text.ToString() + "'");
                }
                if (result)
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功'); window.close()", true);
                }
                else
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('操作失败'); window.location='ProjectModify.aspx'", true);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Div1.Visible = true;
            this.Div2.Visible = false;
            this.Div3.Visible = false;
            this.DropDownList1.Items.Clear();
            for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
            {
                if (this.CheckBoxList1.Items[i].Selected == true)
                {
                    ListItem item = new ListItem();
                    item.Value = this.CheckBoxList1.Items[i].Value.ToString();
                    item.Text = this.CheckBoxList1.Items[i].Text.ToString();
                    this.DropDownList1.Items.Add(item);
                    //this.DropDownList2.Items.Add(this.CheckBoxList1.Items[i]);
                }
            }
            ListItem temitem = new ListItem();
            temitem.Text = "点击选择";
            temitem.Value = "点击选择";
            this.DropDownList1.Items.Add(temitem);
            this.DropDownList1.ClearSelection();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Div1.Visible = true;
            this.Div2.Visible = false;
            this.Div3.Visible = false;
            this.DropDownList2.Items.Clear();
            for (int i = 0; i < this.CheckBoxList2.Items.Count; i++)
            {
                if (this.CheckBoxList2.Items[i].Selected)
                {
                    
                    this.DropDownList2.Items.Add(this.CheckBoxList2.Items[i]);
                }
            }
            ListItem temitem = new ListItem();
            temitem.Text = "点击选择";
            temitem.Value = "点击选择";
            this.DropDownList2.Items.Add(temitem);
            this.DropDownList2.ClearSelection();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DropDownList1.SelectedItem.Value == "点击选择")
            {
                this.Div1.Visible = false;
                this.Div2.Visible = true;
                this.Div3.Visible = false;
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Request.QueryString["ProjectId"] != null)
            {
                modifyProjectId = Request.QueryString["ProjectId"].ToString();
            }
            if (this.DropDownList2.SelectedItem.Value == "点击选择")
            {
                this.CheckBoxList2.Items.Clear();
                this.Div1.Visible = false;
                this.Div2.Visible = false;
                this.Div3.Visible = true;
                DataTable dat = db.GetDataTable("select * from Users ");
                for (int i = 0; i < dat.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dat.Rows[i]["Name"].ToString();
                    item.Value = dat.Rows[i]["Name"].ToString();
                    item.Selected = false;
                    if (dat.Rows[i]["ProjectId"].ToString().Contains(modifyProjectId))
                        item.Selected = true;
                    this.CheckBoxList2.Items.Add(item);
                }
            }
        }
    }
}